<?php
/**
 * Magento
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Academic Free License (AFL 3.0)
 * that is bundled with this package in the file LICENSE_AFL.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/afl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@magentocommerce.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade Magento to newer
 * versions in the future. If you wish to customize Magento for your
 * needs please refer to http://www.magentocommerce.com for more information.
 *
 * @category    design
 * @package     base_default
 * @copyright   Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
 * @license     http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
 */
?>
<?php 
	$orderStatus = array();	
	$orderStatus['unsuccessful'] = array(
		'label'	=> 'Unsuccessful',
		'desc'	=> 'We\'re sorry, your order has been cancelled. If you need help, please call our Gobbler Team.'
	);
	$orderStatus['on_the_way'] = array(
		'label'	=> 'It\'s on the way!',
		'desc'	=> 'We\'re now packing your order and getting it ready for delivery!'
	);
	$orderStatus['delivered'] = array(
		'label'	=> 'Delivered',
		'desc'	=> 'Hooray! Your package has been delivered to your office.'
	);
	$orderStatus['cod'] = array(
		'label'	=> 'Cash on delivery',
		'desc'	=> 'We\'re now packing your order and getting it ready for delivery! Payment is due when your package is delivered.'
	);	
	$orderStatus['refunded'] = array(
		'label'	=> 'Refunded',
		'desc'	=> 'We\'ve just refunded you order.'
	);

	//Unuse statuses
	$orderStatus['processing_payment'] = array(
		'label'	=> 'Processing Payment',
		'desc'	=> 'We\'re currently processing your payment.'
	);
	$orderStatus['processing_order'] = array(
		'label'	=> 'Processing Order',
		'desc'	=> 'Payment processed. We\'re now processing your order.'
	);	
?>

<div class="box-account box-recent">
<?php $_orders = $this->getOrders(); ?>
	
    <div class="box-head">
        <h2 class="double-line"><?php echo $this->__('Shopping History') ?></h2>
        <!--<?php if( sizeof($_orders->getItems()) > 0 ): ?><a href="<?php echo $this->getUrl('sales/order/history') ?>"><?php echo $this->__('View All') ?></a><?php endif; ?>-->
    </div>
<?php if( sizeof($_orders->getItems()) > 0 ): ?>
    <table class="data-table" id="my-orders-table">
		<col />
		<col width="150" />
		<col width="1" />
		<col width="1" />
		<col width="1" />    
		<col width="1" />
        <thead>
            <tr>
                <th class="a-left first"><?php echo $this->__('Order No.') ?></th>
                <th class="a-left"><?php echo $this->__('Date') ?></th>
                <th><?php echo $this->__('Items') ?></th>
                <th><span class="nobr"><?php echo $this->__('Total') ?></span></th>
				<th align="center"><?php echo $this->__('Savings') ?></th>  
                <th class="last" align="left"><?php echo $this->__('Status') ?></th>        
            </tr>
        </thead>
        <tbody>
			<?php 
				$totalSavedAmount = 0;
				$allowStatus = array('processing','paypal_processing','enets_processing','cod','complete','paypal_complete','enets_complete','cod_complete',);
			?>
            <?php foreach ($_orders as $_order): ?>				
				<?php $calculateSave = in_array($_order->getStatus(),$allowStatus) ? true : false ?>				
                <tr>
                    <td class="a-left first">
						<a href="<?php echo $this->getViewUrl($_order) ?>" title="Order <?php echo $_order->getRealOrderId() ?> details">
							<?php echo $_order->getRealOrderId() ?>
						</a> 
						<a href="javascript: void(0);" class="showOrderProduct" id="show-<?php echo $_order->getId()?>">
							<img src="<?php echo $this->getSkinUrl('images/order-small-thumb.png')?>"/>
						</a>
						<?php 
							$orderItems = $_order->getAllVisibleItems();
							$savedAmount = 0;
						?>
						<div id="orderPopup-<?php echo $_order->getId()?>" class="no-display">              
						  <table class="orderTooltipTable">
							<tr>
							<?php foreach($orderItems as $item):?>
								<td>
									<?php 
										$_poption = '';
										$options = $item->getProductOptions();
										if(isset($options['attributes_info'])){
											$_poption .= ' ('.$options['attributes_info'][0]['value'].')';
										}
										
										$product = $item->getProduct();
										
										//if product id is changed, try get product by sku
										if(!$product->getThumbnail()){
											$pid = Mage::getModel('catalog/product')->getIdBySku($item->getSku());
											$product = Mage::getModel('catalog/product')->load($pid);
										}
									?>
									<img src="<?php echo $product->getThumbnailUrl(245, 245)?>" width="245" height="245" alt="<?php echo $item->getName() ?>" />
									<p><?php echo $item->getName().$_poption?></p>									
								</td>
							<?php
								if($calculateSave && ($item->getMsrpPrice() > $item->getPrice())){									
									$savedAmount += ($item->getMsrpPrice() - $item->getPrice()) * $item->getQtyOrdered();			
								}				
							?>
							<?php endforeach?>
							<?php 
								$discountByRuleAmount = $_order->getSubtotal() - $_order->getGrandTotal();
								if($calculateSave && $discountByRuleAmount > 0){
									$savedAmount += $discountByRuleAmount;
								}
							?>
							</tr>
						  </table>						  
						</div>
					</td>
                    <td class="a-left"><span class="nobr"><?php echo $_order->getCreatedAtStoreDate() ?></span></td>
                    <td class="a-center"><?php echo $_order->getTotalQtyOrdered()*1 ?></td>
                    <td class="a-center"><?php echo $_order->formatPrice($_order->getGrandTotal()) ?></td>
                    <td class="a-center">
                        <?php echo $savedAmount > 0 ? $_order->formatPrice($savedAmount) : '$0.00'?>					
                    </td>
					<td class="order-status a-left last">
						<?php //var_dump($_order->getStatus())?>
						<?php 
							$statusCode = '';
							switch($_order->getStatus()){
								case 'canceled':
								case 'pending_payment':
								case 'pending':						
								case 'paypal_pending_payment':							
								case 'enets_pending_payment':							
								// case 'paypal_cancle':
								// case 'paypal_pending':
								// case 'enets_pending':									
									$statusCode = 'unsuccessful';
									break;
								case 'complete':
								case 'paypal_complete':
								case 'enets_complete':
								case 'cod_complete':
									$statusCode = 'delivered';
									break;
								case 'processing':
								case 'enets_processing':
								case 'paypal_processing':
									$statusCode = 'on_the_way';
									break;
								case 'cod':
									$statusCode = 'cod';
									break;
								case 'closed':
									$statusCode = 'refunded';
									break;
								default:
									$statusCode = 'unsuccessful';
									break;
							}
						?>
						<div class="status-label <?php echo $statusCode?>">
							<?php echo $orderStatus[$statusCode] ? $orderStatus[$statusCode]['label'] : 'Missing...'?>							
						</div>
						<div class="status-info no-display">
							<?php echo $orderStatus[$statusCode] ? $orderStatus[$statusCode]['desc'] : 'Missing...'?>
						</div>						
					</td>
                </tr>
				<?php					
					$totalSavedAmount += $savedAmount;
				?>
            <?php endforeach; ?>
        </tbody>
    <tfoot>
      <tr>
        <td colspan="4" class="a-right"><?php echo $this->__('Total Savings'); ?></td> 
        <td class="a-right" style="padding: 0 10px 0 10px;"><?php echo $_order->formatPrice($totalSavedAmount)?></td>
		<td></td>
      </tr>
    </tfoot>
    </table>
    <script type="text/javascript">
		decorateTable('my-orders-table');
		jQuery(document).ready(function($){
			$('.showOrderProduct').each(function() {
				var self = this;
				$(this).qtip({
					content: {
						text: $('#orderPopup-' + self.id.replace('show-',''))
					},
					position : {
						my : 'bottom left',
						at : 'top right',
						adjust : {
							x : 10,
							y: -10
						}
					},
					style: {
						tip: {
							corner: false
						}
					},
					show: {
						event: 'mouseover',
						solo: true // Only show one tooltip at a time
					},
					hide : 'mouseout'
				});
			});
			
			$('.status-label').each(function() {
				var self = this;
				$(this).qtip({
					content: {
						text: $(self).parent().find('.status-info')
					},
					position : {
						my : 'bottom right',
						at : 'top left',
						adjust : {
							x : -5,
							y: 0
						}
					},
					style: {
						tip: {
							corner: false
						},
						classes: 'order-status-info'
					},
					show: {
						event: 'mouseover',
						solo: true // Only show one tooltip at a time
					},
					hide : 'mouseout'
				});
			});
		});
  </script>
<?php else: ?>
	<p><?php echo $this->__('You have placed no orders.'); ?></p>
<?php endif; ?>
</div>
